# coding:utf-8

import pandas as pd
from sklearn.cross_validation import train_test_split
from sklearn.feature_extraction import DictVectorizer
from sklearn.tree import DecisionTreeClassifier
from sklearn import feature_selection
from sklearn.cross_validation import cross_val_score
import numpy as np

titanic = pd.read_csv('http://biostat.mc.vanderbilt.edu/wiki/pub/Main/DataSets/titanic.txt')

y = titanic['survived']
X = titanic.drop(['row.names','name','survived'],axis=1)
X['age'].fillna(X['age'].mean(),inplace=True)
X_train_data , X_test_data , y_train_label , y_test_label = train_test_split(X,y,test_size=0.25,random_state=33)
vec = DictVectorizer()
X_train_vec = vec.fit_transform(X_train_data.to_dict(orient="record"))
X_test_vec = vec.transform(X_test_data.to_dict(orient="record"))
dt = DecisionTreeClassifier(criterion='entropy')
dt.fit(X_train_vec,y_train_label)
print("正确率",dt.score(X_test_vec,y_test_label))


fs = feature_selection.SelectPercentile(feature_selection.chi2,percentile=20)
X_train_fs_vec = fs.fit_transform(X_train_vec,y_train_label)
X_test_fs_vec = fs.fit_transform(X_test_vec)
print("筛选 20 % 特征的正确率： ",dt.score(X_test_fs_vec,y_test_label))
precentiles = range(1,100,2)
for i in precentiles:
    fs = feature_selection.SelectPercentile(feature_selection.chi2,precentile=i)
    X_train_fs_vec1 = fs.fit_transform(X_train_vec,y_train_label)
    scores = cross_val_score(dt,X_train_fs_vec1,y_train_label,cv=5)
    results = np.append(results,scores.mean())

print("results : ")
print(results)
opt = np.where(results == results.max())[0]
print("Optimal number of features %d" %precentiles[opt])